package com.ssm.gbq.controller;

import com.ssm.gbq.model.Flowers;
import com.ssm.gbq.service.FlowersService;
import gbq.ssm.utils.QiniuCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/flowers")
public class FlowersController {

    @Autowired
    private FlowersService flowersService;

    /**
     * 根据条件分页显示花卉信息
     * @param flowers
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showFlowersByPage", method = RequestMethod.POST)
    public HashMap<String, Object> showFlowersByPage(Flowers flowers, int currentPage, int pageSize){
        HashMap<String, Object> hashMap = new HashMap<>();
        //int offset = (currentPage-1)*pageSize;
        try {
            List<Flowers> list = flowersService.showFlowersByPage(flowers, currentPage, pageSize);
            hashMap.put("data", list);
            hashMap.put("count", flowersService.getFlowersCount(flowers));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  hashMap;
    }

    /**
     * 根据ID查询花卉信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getFlowersById")
    public Object getFlowersById(Integer id){
        Flowers flowers = flowersService.getFlowersById(id);
        if(flowers!=null){
            return flowers;
        }else{
            return "未找到信息";
        }
    }

    /**
     * 修改
     * @param flowers
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/updateFlower")
    public HashMap<String, Object> updateFlower(Flowers flowers) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        int i = flowersService.updateFlower(flowers);
        return map;
    }

    /**
     * 添加
     * @param flowers
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/addFlower")
    public HashMap<String, Object> addFlower(Flowers flowers) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        int i = flowersService.addFlower(flowers);
        return map;
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/delFlower")
    public HashMap<String, Object> delFlower(Integer id) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        int i = flowersService.delFlower(id);
        return map;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteFlowers")
    public HashMap<String, Object> deleteFlowers(Integer[] ids){
        HashMap<String, Object> map = new HashMap<>();
        //该方法是将数组转化为list
        flowersService.deleteFlowers(Arrays.asList(ids));
        return map;
    }

    /**
     * 上传图片
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/uploadImg", method=RequestMethod.POST)
    public HashMap<String, Object> uploadImg(@RequestParam MultipartFile img) {
        HashMap<String,Object> result = new HashMap<>();
        if (img.isEmpty()) {
            result.put("status",400);
            result.put("Msg","文件为空，请重新上传");
            return result;
        }
        try {
            byte[] bytes = img.getBytes();
            String imageName = UUID.randomUUID().toString();
            QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
            try {
                //使用base64方式上传到七牛云
                String url = qiniuUtil.put64image(bytes, imageName);
                String urlName = qiniuUtil.getDOMAIN() +"/"+ imageName;
                result.put("Code",200);
                result.put("Msg","文件上传成功");
                result.put("Info",urlName);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        } catch (IOException e) {
            result.put("Code",500);
            result.put("Msg","文件上传发生异常！");
            return result;
        }
    }

}
